package xworker.swt.dataObject.bind.values;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.dataObject.DataObject;
import xworker.swt.dataObject.bind.Value;
import xworker.lang.util.UtilData;

import java.util.ArrayList;
import java.util.List;

public class And implements Value {
    Thing thing;
    List<Value> childs = new ArrayList<>();

    public And(Thing thing){
        this.thing = thing;
    }

    @Override
    public Object getValue(Object value, DataObject dataObject, ActionContext actionContext) {
        boolean checkParent = thing.doAction("isCheckParentValue", actionContext);
        if(checkParent && !UtilData.isTrue(value)){
            return false;
        }

        for(Value v : childs){
            if(!UtilData.isTrue(v.getValue(null, dataObject, actionContext))){
                return false;
            }

        }

        return true;
    }

    public static And create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        And and = new And(self);
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Value){
                and.childs.add((Value) obj);
            }
        }

        return and;
    }
}
